package com.wanmait.wisdom.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.wisdom.entity.Resident;
import com.wanmait.wisdom.manage.service.ManageResidentService;
import com.wanmait.wisdom.mapper.HouseMapper;
import com.wanmait.wisdom.mapper.ResidentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ManageResidentServiceImpl extends ServiceImpl<ResidentMapper, Resident> implements ManageResidentService {

    @Resource
    private ResidentMapper residentMapper;

    @Resource
    private HouseMapper houseMapper;
    //
    @Override
    @Transactional
    public void addResident(Resident resident) {
        residentMapper.insertSelective(resident);
        Integer count = residentMapper.checkPersonSize(resident.getHouseId());
        houseMapper.updatePersonSize(resident.getHouseId(),count);
    }

    @Override
    public Integer getCountByParkingId(Integer parkingId) {
        return  residentMapper.getCountByParkingId(parkingId);
    }

    //查找所有居民
    @Override
    public PageInfo<Resident> findAllResident(Resident searchResident, Integer pageNum, Date startTime, Date endTime, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(residentMapper.findAllResident(searchResident,startTime,endTime),11);
    }

    @Override
    public Resident findResidentById(Integer id) {
        LambdaQueryWrapper<Resident> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Resident::getId,id);
        return residentMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer checkPersonSize(Integer id) {
        return residentMapper.checkPersonSize(id);
    }

    //修改居民信息
    @Override
    @Transactional
    public void updateResident(Resident resident, Integer houseId) {
        //先判断房屋是否更改
        if (resident.getHouseId() ==houseId){
            residentMapper.updateByPrimaryKeySelective(resident);
            houseMapper.updatePersonSize(resident.getHouseId(),residentMapper.checkPersonSize(resident.getHouseId()));
        }
        else{
            houseMapper.updatePersonSize(houseId,residentMapper.checkPersonSize(houseId)-1);
            //如果更改了就先修改居民表的房屋信息，如果没修改就只修改居民信息
            residentMapper.updateByPrimaryKeySelective(resident);
            houseMapper.updatePersonSize(resident.getHouseId(),residentMapper.checkPersonSize(resident.getHouseId()));
            //如果房屋id变了，就修改房屋的住户人数
        }

    }
    //删除居民信息
    @Override
    @Transactional
    public void deleteResident(Integer id, Integer houseId) {
        residentMapper.deleteResident(id);
        Integer count = residentMapper.checkPersonSize(houseId)-1;
        if (count<=0){
            count = 0;
        }
        houseMapper.updatePersonSize(houseId,count);
    }

    @Override
    public Long getResidentCount() {
        QueryWrapper<Resident> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable",1);
        return residentMapper.selectCount(queryWrapper);
    }


}
